<!--
 * @Descripttion: 预约服务
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-28 17:38:48
-->
<template>
    <div class="lb-appointment-service-list">
        <top-nav></top-nav>
        <div class="page-main">
          <el-row class="page-top-operate">
              <lb-button size='medium' type='primary' icon='el-icon-plus' @click='addEditSetviceInfo("add")'>新增预约服务</lb-button>
          </el-row>
          <el-table
          v-loading='loading'
            :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
            style="width: 100%">
              <el-table-column
                label="缩略图">
                <template slot-scope="scope">
                  <lb-image  :src="scope.row.cover[0]" />
                </template>
              </el-table-column>
              <el-table-column
                prop="title"
                label="服务名称">
              </el-table-column>
              <el-table-column
                prop="cate_text"
                label="服务分类">
              </el-table-column>
              <el-table-column
                width="400px"
                label="可约日期">
                <template slot-scope="scope">
                  <span>{{scope.row.days_text.join(',')}}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="top"
                label="排序值">
              </el-table-column>
              <el-table-column
                label="是否启用">
                  <template slot-scope="scope">
                    <el-switch
                    v-model="scope.row.status"
                    :active-value="1"
                    :inactive-value="0"
                    @change="updateService(scope.row.status,scope.row.id)"
                    ></el-switch>
                  </template>
              </el-table-column>
              <el-table-column
                prop="status"
                label="操作">
                  <template slot-scope="scope">
                    <div class="table-operate">
                      <lb-button type='primary' size='mini' plain @click="addEditSetviceInfo('edit', scope.row.id)">{{$t('action.edit')}}</lb-button>
                      <lb-button type='danger' size='mini' plain @click="confirmDel(scope.row.id)">{{$t('action.delete')}}</lb-button>
                    </div>
                  </template>
              </el-table-column>
        </el-table>
        <lb-page
          :batch='false'
          :page='page'
          :pageSize='limit'
          :total='total'
          @handleSizeChange='handleSizeChange'
          @handleCurrentChange='handleCurrentChange'
          >
        </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      page: 1,
      limit: 10,
      total: 0,
      tableData: [],
      loading: false
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList () {
      this.loading = true
      let {page, limit} = this
      this.$api.getAppointmentServiceList({page, limit}).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    addEditSetviceInfo (type, id) {
      let url = type === 'add' ? '/app/appointment/editService' : `/app/appointment/editService?id=${id}`
      this.$router.push(url)
    },
    updateService (status, id) {
      this.$api.updateAppointmentStatus({status, id}).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status === -1) {
            this.page = this.page < Math.ceil((this.total - 1) / this.limit) ? this.page : Math.ceil((this.total - 1) / this.limit)
          }
          this.getTableDataList()
        }
      })
    },
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateService(-1, id)
      })
    },
    handleSizeChange (val) {
      this.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getTableDataList()
    }
  }
}
</script>

<style lang="scss" scoped>
    .lb-appointment-service-list{
        width: 100%;

    }
</style>
